package com.deyuanyun.pic.pipe.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.pipe.controller.dto.ValveQuery;
import com.deyuanyun.pic.pipe.controller.vo.ValveVO;
import com.deyuanyun.pic.pipe.enumeration.PipeExportEnum;
import com.deyuanyun.pic.pipe.service.PipeMgrService;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * “线路阀门”控制类
 *
 * @作者:严祥
 * @创建时间: 2015年9月14日
 * @类名称: PipeValveController
 *
 */
@Controller
@RequestMapping("/pipe/valve")
public class ValveController extends PipeBaseController {

    @Resource
    private PipeMgrService pipeMgrService;

    /**
     * 跳转到 线路阀门 主页面
     *
     * @return
     */
    @RequestMapping("/toValve")
    public ModelAndView toValve() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/basedata/pipe/pipeValve");
        return mv;
    }

    /**
     * 根据条件分页查询 线路阀门 数组对象
     *
     * @param valveQuery
     */
    @RequestMapping("/query.json")
    @ResponseBody
    public void query(ValveQuery valveQuery) {
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(this.queryVO(valveQuery));
        AjaxSupport.sendSuccessText("success", jsonBean);
    }

    /**
     * 根据条件导出 线路阀门
     *
     * @param valveQuery
     * @throws IOException
     */
    @RequestMapping("/export.json")
    @ResponseBody
    public void export(ValveQuery valveQuery) throws IOException {
        List list = this.queryVO(valveQuery);
        PipeExportEnum exportEnum = PipeExportEnum.Valve;
        String[] columnKeys = {"pipeLineName", "valvaNumber", "manufacturedNum", "valveStatus", "valveType",
                "operatorType", "pipeValveroomName", "mileage", "automated", "inletConnectionType",
                "inletDiameter", "inletWallThickness", "outletConnectionType", "outletDiameter", "outletWallThickness",
                "pressureRating",  "valvefunction", "indremotecon", "manufacturer", "manufacturedDate",
                "inserviceDate", "constructionCompany", "inspectingCompany", "testingCompany", "recorder", "recordTime", "remark"};
        DownExcelUtil.export(exportEnum.getFileName(), pipePath+exportEnum.getTempPath(), exportEnum.getBeginRow(), list, columnKeys);
    }

    private List queryVO(ValveQuery valveQuery) {
        return new ValveVO().fromDomainList(pipeMgrService.queryValve(valveQuery));
    }
}
